package util.content;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

/*这个类的作用：拿出xml中正则抓取需要的参数，把网页的文件bodyString和正则规则传给ContentCapture类，并调用ContentCapture从ContentCapture中获得抓取结果*/

public class ContentRex {

	List sourceSiteList = null;

	public String getTitle() {
		for (Iterator iterSourceSite = sourceSiteList.iterator(); iterSourceSite.hasNext();) {
			Element sourceSite = (Element) iterSourceSite.next();
			String titleRex = sourceSite.getChildText("titleRex");
			Pattern p = Pattern.compile(titleRex);
			Matcher m = p.matcher(title);
			if (m.find()) {
				title = m.group(1).trim();
			}
		}
		return title;
	}

	String contentString = "";

	public String getContentString() {
		contentString = "";
		ContentCapture cc;
		for (Iterator iterSourceSite = sourceSiteList.iterator(); iterSourceSite.hasNext();) {
			Element sourceSite = (Element) iterSourceSite.next();
			// 拿取可以识别是否在回答中的问题的标记字符串，并在bodyString中检索发现了点话说明正在回答中不抓取但做一个标记
			String sloveRex = sourceSite.getChildText("sloveRex");
			Pattern slovePattern = Pattern.compile(sloveRex);
			Matcher sloveMatcher = slovePattern.matcher(bodyString);
			sloveMatcher.matches();
			sloveMatcher.reset();
			// 抓取解决问题
			if (sloveMatcher.find()) {
				
				List spiderSloveList = sourceSite.getChildren("spiderSlove");
				for (Iterator iterSpider = spiderSloveList.iterator(); iterSpider.hasNext();) {
					Element spider = (Element) iterSpider.next();
					List contentList = spider.getChildren("content");
					for (Iterator iterContent = contentList.iterator(); iterContent.hasNext();) {
						Element contentE = (Element) iterContent.next();
						// 初始化乱序类rc和正则抓取类cc
						cc = new ContentCapture();
						// 拿出xml文件中的参数
						String contentArea = contentE.getChildTextTrim("contentArea");
						String oneContentArea = contentE.getChildTextTrim("oneContentArea");
						String mainArea = contentE.getChildTextTrim("mainArea");
						String mainRex = contentE.getChildTextTrim("mainRex");
						String contentAttri = contentE.getAttributeValue("id");
						// 用contentArea截取出整个问题、最佳或普通回答的区块
						cc.setContentArea(contentArea);	
						cc.setMainArea(mainArea);
						cc.setOneContentArea(oneContentArea);
						cc.setMainRex(mainRex);
						cc.setContentAttri(contentAttri);
						
						contentString = contentString + cc.getContent(bodyString);
						
						cc = null;
					}
				}
			}
			// 正在回答中的字符串返回9527作为标记
			else {
				contentString = "9527";
			}
		}
		return contentString;
	}

	String bodyString = null;

	public void setBodyString(String bodyString) {
		this.bodyString = bodyString;
	}

	String title = null;

	public void setTitle(String title) {
		this.title = title;
	}
	
	ContentRex() {

		try {
			String xmlpath = "spiderParameter.xml";
			SAXBuilder builder = new SAXBuilder(false);
			Document doc = builder.build(xmlpath);
			Element sourceSite = doc.getRootElement();
			sourceSiteList = sourceSite.getChildren("sourceSite");
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
